<template>
  <view v-if="template">
    <s-layout
      title="首页"
      navbar="custom"
      tabbar="/pages/index/index"
      :bgStyle="template.style?.background"
      :navbarStyle="template.style?.navbar"
      onShareAppMessage
    >
      <s-block v-for="(item, index) in template.data" :key="index" :styles="item.style">
        <s-block-item :type="item.type" :data="item.data" :styles="item.style" />
      </s-block>
      <!-- 广告模块 -->
      <s-popup-image />
    </s-layout>
  </view>
</template>

<script setup>
  import { computed } from 'vue';
  import { onLoad, onPageScroll, onPullDownRefresh } from '@dcloudio/uni-app';
  import sheep from '@/sheep';
  import $share from '@/sheep/platform/share';

  // 隐藏原生tabBar
  uni.hideTabBar();

  const template = computed(() => {
    let home = sheep.$store('app').template?.home;
    home.data.forEach((item, index) => {
      let title = item.data?.title?.text || '';
      if (item.type == "titleBlock" && (title == '新闻' || title == '活动' || title == '公告')) {
        home.data.splice(index + 1, 0, {
          type: "news",
          data: {
            title, // 标题（类型）
            dots: false, // 显示指示点
            color: "#979797", // 指示点颜色
            activeColor: "#e7bd7d", // 指示点选中颜色
            circular: true, // 循环播放
            autoplay: true, // 自动播放
            interval: 5000, // 轮播间隔 ms
            duration: 500, // 滑动动画时长 ms
            height: 200, // 轮播图高度
          },
          style: {
            background: {
              bgColor: "#FFFFFF",
              bgImage: "",
              type: "color"
            },
            borderRadiusBottom: 8,
            borderRadiusTop: 8,
            marginBottom: 10,
            marginLeft: 10,
            marginRight: 10,
            marginTop: 0,
            padding: 0,
          }
        })
      }
    })
    return home
  });
  

  console.log(template);

  onLoad((options) => {
    // #ifdef MP
    // 小程序识别二维码
    if (options.scene) {
      const sceneParams = decodeURIComponent(options.scene).split('=');
      options[sceneParams[0]] = sceneParams[1];
    }
    // #endif

    // 预览模板
    if (options.templateId) {
      sheep.$store('app').init(options.templateId);
    }

    // 解析分享信息
    if (options.spm) {
      $share.decryptSpm(options.spm);
    }

    // 进入指定页面(完整页面路径)
    if (options.page) {
      sheep.$router.go(decodeURIComponent(options.page));
    }
  });

  // 下拉刷新
  onPullDownRefresh(() => {
    sheep.$store('app').init();
    setTimeout(function () {
      uni.stopPullDownRefresh();
    }, 800);
  });

  onPageScroll(() => {});
</script>

<style></style>
